import ballerina/http;

map<json> phrases = {
    "Good morning, how are you?": {
        "EN": "Good morning, how are you?",
        "DE": "Guten Morgen, wie geht es dir?",
        "OS": "Walala po, onawa tuu?",
        "OT": "Wapenduka nawa?"
    },
    "I'm doing fine.": {
        "EN": "I'm doing fine.",
        "DE": "Ich bin wohlauf.",
        "OS": "Ondili nawa.",
        "OT": "Mbiri nawa."
    },
    "Can you please assist me with some water?": {
        "EN": "Can you please assist me with some water?",
        "DE": "Können Sie mir bitte etwas Wasser geben?",
        "OS": "Oto kwathele ndje omeya?",
        "OT": "Mbwaye, mbivatera omeva?"
    },
    "Thank you very much!!": {
        "EN": "Thank you very much!!",
        "DE": "Vielen Dank!!",
        "OS": "Tangi unene!!",
        "OT": "Okuhepa!!"
    }
};

listener http:Listener h1 = new http:Listener(9000, config = {httpVersion:"1.1"});
listener http:Listener h2 = new http:Listener(9001, config = {httpVersion:"2.0"});

service Translator on h2 {
    http:Response r = new http:Response();
    @http:ResourceConfig {
        methods: ["GET"],
        path: "/{languageCode}/{phrase}"
    }
    resource function translate(http:Caller caller, http:Request request, string languageCode, string phrase) {
        http:Client clientEndpoint = new("http://localhost:9000");
        var response = clientEndpoint->get(untaint("/"+languageCode+"/"+phrase));
        if (response is error) {
            // 🤷
        } else {
            string | error r = response.getTextPayload();
            if (r is string) {
                error? result = caller->respond(untaint(r));
            }
        }
    }
}

service EnglishAssistant on h1 {
    @http:ResourceConfig {
        methods: ["GET"],
        path: "/EN/{phrase}"
    }
    resource function translate(http:Caller caller, http:Request request, string phrase) {
        error | json translation = phrases[phrase]["EN"];
        http:Response response = new http:Response();
        if translation is json {
            response.setJsonPayload(translation, contentType = "application/json");
        } else {
            response.setPayload("Something went wrong");
        }
        var x = caller ->respond(response);
    }  
}

service GermanAssistant on h1 {
    @http:ResourceConfig {
        methods: ["GET"],
        path: "/DE/{phrase}"
    }
    resource function translate(http:Caller caller, http:Request request, string phrase) {
        error | json translation = phrases[phrase]["DE"];
        http:Response response = new http:Response();
        if translation is json {
            response.setJsonPayload(translation, contentType = "application/json");
        } else {
            response.setPayload("Something went wrong");
        }
        var x = caller ->respond(response);
    }  
}

service OshiwamboAssistant on h1 {
    @http:ResourceConfig {
        methods: ["GET"],
        path: "/OS/{phrase}"
    }
    resource function translate(http:Caller caller, http:Request request, string phrase) {
        error | json translation = phrases[phrase]["OS"];
        http:Response response = new http:Response();
        if translation is json {
            response.setJsonPayload(translation, contentType = "application/json");
        } else {
            response.setPayload("Something went wrong");
        }
        var x = caller ->respond(response);
    }  
}

service OtjihereroAssistant on h1 {
    @http:ResourceConfig {
        methods: ["GET"],
        path: "/OT/{phrase}"
    }
    resource function translate(http:Caller caller, http:Request request, string phrase) {
        error | json translation = phrases[phrase]["OT"];
        http:Response response = new http:Response();
        if translation is json {
            response.setJsonPayload(translation, contentType = "application/json");
        } else {
            response.setPayload("Something went wrong");
        }
        var x = caller ->respond(response);
    }  
}